package com.itheima.tlias.service.impl;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias.mapper.EmpExprMapper;
import com.itheima.tlias.mapper.EmpMapper;
import com.itheima.tlias.model.dos.EmpAndExprDo;
import com.itheima.tlias.model.dos.EmpDO;
import com.itheima.tlias.model.dto.EmpAddDTO;
import com.itheima.tlias.model.dto.EmpQueryDTO;
import com.itheima.tlias.model.entity.Emp;
import com.itheima.tlias.model.entity.EmpExpr;
import com.itheima.tlias.model.response.EmpExprResult;
import com.itheima.tlias.model.response.PageResult;
import com.itheima.tlias.service.EmpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl implements EmpService {
    final
    EmpMapper empMapper;

    public EmpServiceImpl(EmpMapper empMapper) {
        this.empMapper = empMapper;
    }
    @Autowired
    EmpExprMapper empExprMapper;


    @Override
    @Transactional
    public void deleteEmp(List<Integer> id) {
        empMapper.deleteEmp(id);
        empExprMapper.deleteEmppExpr(id);
    }

    @Override
    public EmpAndExprDo selectById(Integer id) {
        EmpAndExprDo emp = empMapper.selectById(id);
      return emp;
    }

    @Override
    @Transactional
    public void updata(EmpAddDTO dto) {
        Emp emp = new Emp();
        BeanUtils.copyProperties(dto,emp);
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updata(emp);



        //获取员工工作经历
        Integer id = emp.getId();
        List<EmpExpr> exprList = dto.getExprList();
        //判断工作经历不为空时添加
        if(!ObjectUtils.isEmpty(exprList)){

            empExprMapper.deleteEmppExpr(Arrays.asList(emp.getId()));


        }

    }

    @Override
    public List<Emp> emplist() {
        List<Emp> emp =  empExprMapper.emplist();
        return emp;
    }


    @Override
    public PageResult<EmpDO> empListPageQuery(EmpQueryDTO dto) {
        //参数校验


        //逻辑处理
        Page<EmpDO> page = PageHelper.startPage(dto.getPage(), dto.getPageSize())
                .doSelectPage(() -> {
                    empMapper.selectByPage(dto);
                });

        //返回
        return new PageResult<>(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional

    public void add(EmpAddDTO dto) {

        Emp emp = new Emp();
        BeanUtils.copyProperties(dto,emp);
        //添加员工信息表

        emp.setCreateTime(LocalDateTime.now());

        emp.setUpdateTime(LocalDateTime.now());
        empMapper.addEmp(emp);


        //获取员工工作经历
        List<EmpExpr> exprList = dto.getExprList();
        //判断工作经历不为空时添加
        if(!ObjectUtils.isEmpty(exprList)){
            List<EmpExpr> empExprlist = exprList.stream().map((o) -> {
                EmpExpr empExpr = new EmpExpr();
                BeanUtils.copyProperties(o, empExpr);
                empExpr.setEmpId(emp.getId());
                return empExpr;
            }).collect(Collectors.toList());
            empExprMapper.addEmpexer(empExprlist);
        }
    }


}
